package edu.uoc.tds.pac4.client.ss2;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
 * Custom render per al calendari 
 */
public class DiaCalendariRenderer extends JLabel
implements TableCellRenderer {
	private static final long serialVersionUID = 1L;

	public DiaCalendariRenderer() {
		//MUST do this for background to show up.
		setOpaque(true); 
	}

	/**
	 * Render per una taula calendari
	 */
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		
		
		if(value != null) {
			DiaCalendari dia = (DiaCalendari)value;
			
			if(dia.events != null && dia.events.size() > 0) {
				switch(dia.events.size()) {
				case 1:
					setBackground(Color.YELLOW);
					break;
				case 2:
					setBackground(Color.ORANGE);
					break;
				default:
					setBackground(Color.RED);
					break;
				}
				
				toolTipText(dia);
			}
			else 
				setBackground(Color.WHITE);
			
			setText(dia.getDia());
			
		}
		else {
			setBackground(Color.LIGHT_GRAY);
			setText("");
			setToolTipText("");
		}
		return this;
	}
	
	protected void toolTipText(DiaCalendari dia)
	{
		String text = "<html>";
		
		for(int i=0;i<dia.events.size();i++)
			text += dia.events.get(i).getDescripcio() + "<br>";
		
		text += "</html>";
		setToolTipText(text);
	}
}
